package com.ssm.lzh.service.impl;

import com.ssm.lzh.service.WorktableService;
import com.ssm.lzh.vo.WorktableView;
import com.ssm.mapper.WorktableMapper;
import com.ssm.pojo.Physique;
import com.ssm.pojo.Psychology;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class WorktableServiceImpl implements WorktableService {

    @Resource
    private WorktableMapper worktableMapper;


    @Override
    public WorktableView queryAllData() {
        WorktableView w=new WorktableView();

        DateFormat dateFormat = new SimpleDateFormat("yyyy-M-d");
        String data=dateFormat.format(new Date()).toString();
        //查询今日预约人数
        w.setTodayBookingNumber(this.worktableMapper.queryTodayBookingNumber(data));
        List<Physique> physiques = this.worktableMapper.queryAllPgPhysique();
        List<Psychology> psychologies = this.worktableMapper.queryAllPgPsychology();
        //今日评估人数
        w.setTodayPgNumber(queryTodayPgNumber(physiques,psychologies));
        //评估人数
        w.setPgSum(setPgsum(physiques,psychologies));
        //预约人数
        w.setBookingSum(this.worktableMapper.queryBookingSum().size());
        // 今日新增会员
        w.setTodayAddMember(this.worktableMapper.queryTodayAddMember(data));
        //今日登陆会员
        w.setTodayLoginMember(this.worktableMapper.queryTodayLoginMember(data));
        // 会员总数
        w.setMemberSum(this.worktableMapper.queryMemberSum());
        return w;
    }

    private int setPgsum(List<Physique> physiques, List<Psychology> psychologies) {
        List<String> todatphysiquess = new ArrayList<String>();
        List<String> todatpsychologiess = new ArrayList<String>();
        for (Physique p : physiques
        ) {
            todatphysiquess.add(p.getId());
        }
        for (Psychology p:psychologies
        ) {
            todatpsychologiess.add(p.getId());
        }
        List<String> collect = Stream.of(todatphysiquess, todatpsychologiess)
                .flatMap(Collection::stream)
                .distinct()
                .collect(Collectors.toList());

        return collect.size();
    }

    public int queryTodayPgNumber(List<Physique> l,List<Psychology> l2){
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String data=dateFormat.format(new Date()).toString();
        List<String> todats=new ArrayList<String>();
        List<String> todats2=new ArrayList<String>();
        for (Physique p:l
             ) {
            if (p.getDate().equals(data)){
                todats.add(p.getId());
            }
        }
        for (Psychology p:l2
        ) {
                    if (p.getDate().equals(data)){
                        todats2.add(p.getId());
                    }
        }
        List<String> collect = Stream.of(todats, todats2)
                .flatMap(Collection::stream)
                .distinct()
                .collect(Collectors.toList());
        return collect.size();
    }
}
